package io.wongxd.solution.permission

import android.content.pm.PackageManager
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import java.util.concurrent.atomic.AtomicInteger
import kotlin.properties.Delegates


//activity.reqPermission(Manifest.permission.CAMERA, onPermit = {
//    //申请权限成功 Do something
//}, onDeny = { shouldShowCustomRequest ->
//    //申请权限失败 Do something
//    if (shouldShowCustomRequest) {
//        //用户选择了拒绝并且不再询问，此时应该使用自定义弹窗提醒用户授权（可选）
//    }
//})
object PermissionHelper {
    private val nextLocalRequestCode = AtomicInteger()

    private val nextKey: String
        get() = "activity_rq#${nextLocalRequestCode.getAndIncrement()}"

    fun FragmentActivity.reqPermissionSingle(
        permission: String,
        onPermit: () -> Unit,
        onDeny: (shouldShowCustomRequest: Boolean) -> Unit
    ) {
        if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
            onPermit()
            return
        }
        var launcher by Delegates.notNull<ActivityResultLauncher<String>>()
        launcher = activityResultRegistry.register(
            nextKey,
            ActivityResultContracts.RequestPermission()
        ) { result ->
            if (result) {
                onPermit()
            } else {
                onDeny(!ActivityCompat.shouldShowRequestPermissionRationale(this, permission))
            }
            launcher.unregister()
        }
        lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    launcher.unregister()
                    lifecycle.removeObserver(this)
                }
            }
        })
        launcher.launch(permission)
    }

    fun ComponentActivity.reqPermissionBatch(
        permissions: Array<String>,
        onPermit: () -> Unit,
        onDeny: (shouldShowCustomRequest: Boolean) -> Unit
    ) {
        var hasPermissions = true
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(
                    this,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                hasPermissions = false
                break
            }
        }
        if (hasPermissions) {
            onPermit()
            return
        }
        var launcher by Delegates.notNull<ActivityResultLauncher<Array<String>>>()
        launcher = activityResultRegistry.register(
            nextKey,
            ActivityResultContracts.RequestMultiplePermissions()
        ) { result ->
            var allAllow = true
            for (allow in result.values) {
                if (!allow) {
                    allAllow = false
                    break
                }
            }
            if (allAllow) {
                onPermit()
            } else {
                var shouldShowCustomRequest = false
                for (permission in permissions) {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                        shouldShowCustomRequest = true
                        break
                    }
                }
                onDeny(shouldShowCustomRequest)
            }
            launcher.unregister()
        }
        lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    launcher.unregister()
                    lifecycle.removeObserver(this)
                }
            }
        })
        launcher.launch(permissions)
    }
}